package cn.chiship.framework.third.biz.wxpub.service.impl;

import cn.chiship.framework.third.biz.wxpub.entity.*;
import cn.chiship.framework.third.biz.wxpub.mapper.ThirdWechatArticleMapper;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.util.StringUtil;
import cn.chiship.sdk.framework.base.BaseServiceImpl;
import cn.chiship.framework.third.biz.wxpub.mapper.ThirdWechatArticleRelationConfigMapper;
import cn.chiship.framework.third.biz.wxpub.service.ThirdWechatArticleRelationConfigService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 素材关联配置业务接口实现层
 * 2022/9/17
 * @author lijian
 */
@Service
public class ThirdWechatArticleRelationConfigServiceImpl extends BaseServiceImpl<ThirdWechatArticleRelationConfig,ThirdWechatArticleRelationConfigExample> implements ThirdWechatArticleRelationConfigService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ThirdWechatArticleRelationConfigServiceImpl.class);

    @Resource
    ThirdWechatArticleRelationConfigMapper thirdWechatArticleRelationConfigMapper;

    @Resource
    ThirdWechatArticleMapper thirdWechatArticleMapper;

    @Override
    public BaseResult list(String relationConfigId) {
        ThirdWechatArticleRelationConfigExample wechatArticleRelationConfigExample = new ThirdWechatArticleRelationConfigExample();
        ThirdWechatArticleRelationConfigExample.Criteria criteria = wechatArticleRelationConfigExample.createCriteria();
        if (!StringUtil.isNullOrEmpty(relationConfigId)) {
            criteria.andRelationConfigIdEqualTo(relationConfigId);
        }
        List<JSONObject> results = new ArrayList<>();
        List<ThirdWechatArticleRelationConfig> wechatArticleRelationConfigs = thirdWechatArticleRelationConfigMapper.selectByExample(wechatArticleRelationConfigExample);
        List<String> articleIds = new ArrayList<>();
        for (ThirdWechatArticleRelationConfig wechatArticleRelationConfig : wechatArticleRelationConfigs) {
            articleIds.add(wechatArticleRelationConfig.getArticleId());
            results.add(JSON.parseObject(JSON.toJSONString(wechatArticleRelationConfig)));
        }
        if (!articleIds.isEmpty()) {
            ThirdWechatArticleExample thirdWechatArticleExample = new ThirdWechatArticleExample();
            thirdWechatArticleExample.createCriteria().andIdIn(articleIds);
            List<ThirdWechatArticle> thirdWechatArticles = thirdWechatArticleMapper.selectByExample(thirdWechatArticleExample);
            for (JSONObject json : results) {
                List<ThirdWechatArticle> articles = thirdWechatArticles.stream().filter(article -> article.getId().equals(json.getString("articleId"))).collect(Collectors.toList());
                if (!articles.isEmpty()) {
                    json.put("article", articles.get(0));
                }
            }
        }

        return BaseResult.ok(results);
    }
}
